package DataStructure.TreeLearning.Demo1;

class Tree {
    public static void main(String[] args) {
        TreeNode root = new TreeNode(new TreeNode(new TreeNode(new TreeNode(null, 4, null), 3, null), 2, null), 1,
                new TreeNode(new TreeNode(null, 6, new TreeNode(null, 7, null)), 5, null));
        readTree(root);
    }

    // 中序遍历
    // 通过调整print语句的位置可以更改为先序遍历或后序遍历
    public static void readTree(TreeNode n) {
        if(n.leftChild != null) {
            readTree(n.leftChild);
        }
        System.out.print(n.value + " ");
        if(n.rightChild != null) {
            readTree(n.rightChild);
        }
    }
}

class TreeNode {
    TreeNode leftChild;
    int value;
    TreeNode rightChild;

    public TreeNode(TreeNode leftChild, int value, TreeNode rightChild) {
        this.leftChild = leftChild;
        this.value = value;
        this.rightChild = rightChild;
    }
}